import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lahiru
 * Date: 3/24/14
 * Time: 10:29 PM
 * To change this template use File | Settings | File Templates.
 */

/*
 The Usage : java SegmentTextFile data/train_si284/spk2gender data/train_si284/spk2utt data/train_si284/text
 */
public class SegmentTextFile {
    public static void main(String[] args) {
        String spk2gender = args[0];
        String spk2utt = args[1];
        String text = args[2];


        ArrayList<String> female = new ArrayList<String>();
        ArrayList<String> male = new ArrayList<String>();
        ArrayList<String> speakers = new ArrayList<String>();
        ArrayList<String> utterances = new ArrayList<String>();     // line is speaker_id utt1 utt2 utt3
        Map<String, String> textList = new HashMap<String, String>();

        // Read the spk2gender file to select 40 speakers
        try {
            BufferedReader br = new BufferedReader(new FileReader(spk2gender));
            String line;
            while ((line = br.readLine()) != null) {
                String gender = (line.split(" "))[1];
                if (gender.equalsIgnoreCase("f")) {
                    female.add((line.split(" "))[0]);
                } else if (gender.equalsIgnoreCase("m")) {
                    male.add((line.split(" "))[0]);
                }
//                System.out.println(gender);
//                System.out.println(line);
            }
            br.close();

            for (int i = 0; i < 20; i++) {
                speakers.add(female.get(i));
            }

            for (int i = 0; i < 20; i++) {
                speakers.add(male.get(i));
            }

//            for(String spk : speakers){
//                System.out.println(spk);
//            }
            System.out.println("The number of females = " + female.size());
            System.out.println("The number of males = " + male.size());
            System.out.println("The number of speakers = " + speakers.size());

            //Read the spk2utt file and store utterances list for 40 speakers
            BufferedReader br1 = new BufferedReader(new FileReader(spk2utt));
            String line1;
            while ((line1 = br1.readLine()) != null) {
                String spker = (line1.split(" "))[0];
                if (speakers.contains(spker)) {
                    utterances.add(line1);
                }
            }
            br1.close();


            // Read the wav.scp file and store its values in the fullUtteranceList
            BufferedReader br2 = new BufferedReader(new FileReader(text));
            String line2;
            while ((line2 = br2.readLine()) != null) {
                String wavArray[] = line2.split(" ");
                StringBuffer value = new StringBuffer();
                for (int k = 1; k < wavArray.length; k++) {
                    value.append(wavArray[k] + " ");
                }
                textList.put(wavArray[0], value.toString());
            }
            System.out.println("The size of the textList " + textList.size());
            br2.close();


            for (String utt : utterances) {
                String uttArray[] = utt.split(" ");
                String speakerId = uttArray[0];
                String dirName = "speaker-effects/data/" + speakerId;

                //create a dir with the name of the speaker id
                File theDir = new File(dirName);

                // if the directory does not exist, create it
//            if (!theDir.exists()) {
////                    System.out.println("creating directory: " + dirName);
//                boolean result = theDir.mkdir();
//
//                if(result) {
////                        System.out.println("DIR created");
//                }
//            }


                FileWriter fstream = new FileWriter(dirName + "/text", true);
                BufferedWriter out = new BufferedWriter(fstream);
                for (int j = 1; j < uttArray.length; j++) {
                    String key = uttArray[j];
                    String value = textList.get(key);
                    System.out.println(" the key is " + key);
                    System.out.println(" the value is " + value);
                    out.write(key + " " + value + "\n");
                }
                out.close();
//                System.out.println(uttArray.length);
            }
            System.out.println("size of utterances " + utterances.size());
//        StringBuffer speakerBuffer = new StringBuffer();
//        for(String spk : speakers){
//            speakerBuffer.append(spk+" ");
//        }
//        System.out.println("The list of speakers ");
//        System.out.println(speakerBuffer.toString());


        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
